package com.example.ponycui_home.svgaplayer;

import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGAVideoEntity;
import com.opensource.svgaplayer.iml.ParseCompletion;
import com.opensource.svgaplayer.utils.log.LogUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;
import java.util.ArrayList;

/**
 * ex.
 *
 * @author wl
 * @since 2021-06-22
 */
public class AnimationFromAssetsAbility extends Ability {
    private static int nextIndex = 0;
    int currentIndex = 0;
    SVGAImageView animationView = null;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor("#FF000000"));
        setUIContent(ResourceTable.Layout_ability_animation_from_assets);
        DirectionalLayout root = (DirectionalLayout)findComponentById(ResourceTable.Id_root);
        animationView = new SVGAImageView(this);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFF000000));
        animationView.setImageElement(shapeElement);
        animationView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                animationView.stepToFrame(currentIndex++, false);
            }
        });
        loadAnimation();
        root.addComponent(animationView, ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_PARENT);
    }
    private void loadAnimation() {
        SVGAParser svgaParser = SVGAParser.shareParser();
        String name = this.randomSample();
        svgaParser.setFrameSize(100, 100);
        svgaParser.decodeFromAssets(name, new ParseCompletion() {
            @Override
            public void onComplete( SVGAVideoEntity videoItem) {
                animationView.setVideoItem(videoItem);
                animationView.stepToFrame(0, true);
            }

            @Override
            public void onError() {

            }
        });
        nextIndex++;
    }

    @Override
    protected void onStop() {
        super.onStop();
        animationView.stopAnimation(true);
    }

    private ArrayList<String> samples = new ArrayList();


    private String randomSample() {
        if (samples.size() == 0) {
            samples.add("750x80.svga");
            samples.add("alarm.svga");
            samples.add("angel.svga");
            samples.add("Castle.svga");
            samples.add("EmptyState.svga");
            samples.add("Goddess.svga");
            samples.add("gradientBorder.svga");
            samples.add("heartbeat.svga");
            samples.add("matteBitmap.svga");
            samples.add("matteBitmap_1.x.svga");
            samples.add("matteRect.svga");
            samples.add("MerryChristmas.svga");
            samples.add("posche.svga");
            samples.add("Rocket.svga");
            samples.add("rose.svga");
            samples.add("rose_2.0.0.svga");
        }
        return samples.get( nextIndex%samples.size());
    }
}
